﻿using UnityEngine;
using System.Collections;
using System.IO;
using System;


public class StreamDemo0 : MonoBehaviour {

	string str=string.Empty;
	string strrr=string.Empty;
	


	//the code in start() is  for  using streamReader and Stream class to get txt file and show the content.
	//We use 2 ways.  One for using StreamReader directly another  for using stream and StreamReader.

	// This example can be compared with the s_TCP.cs script which is used  for network use. 
	//Both examples use  stream 


	//we have networkStream  FileStream  or other Streams.


	// Use this for initialization
	void Start () 
	{
	  try{

       // FileStream fstr=new FileStream(
		string path=Application.dataPath+"/../testTxt0.txt";

			//method 1  use  StreamReader  directly
		StreamReader sr=new StreamReader(path);
		string tmpStr=string.Empty;
		//while( (tmpStr=sr.ReadLine())!=null );
		//{
			tmpStr=sr.ReadLine();
			str+=tmpStr;
		//}

		Debug.LogWarning(str);
			sr.Close();

			//method 2  use  StreamReader  and  a stream

			FileStream fstr=new FileStream(path,FileMode.Open);
			StreamReader sr2=new StreamReader(fstr);

			string tmpStr2=string.Empty;
			while( (tmpStr2=sr2.ReadLine())!=null )
			{
				strrr+=tmpStr2;
			}

			Debug.LogWarning("stream related way: "+ strrr);


		}
		catch(Exception e  )
		{
			Debug.LogError(" Error when reading txt file: "+e.ToString()   );
		}


	



	}
	
	// Update is called once per frame
	void Update () {
	
		//Application.pa


	}
}
